package com.cpi.cornpp.versionsvalues.converter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.versionsvalues.dto.VersionsValuesDTO;
import com.cpi.cornpp.versionsvalues.exception.VersionsValuesException;
import com.cpi.cornpp.versionsvalues.view.VersionsValuesView;
import com.cs.common.utils.NumberUtil;

/**
 * Versions Values Converter
 * @author Edgar Joao
 *
 */
public class VersionsValuesConverter {
    /**
     * Versions Values Converter DTO to View
     * @param dto
     * @return
     * @throws VersionsValuesException
     */
	public VersionsValuesView converterVersionsValuesDtoToView(VersionsValuesDTO dto) throws VersionsValuesException{
		VersionsValuesView view = null;
		try{
			if(dto != null){
				view = new VersionsValuesView();				
				view.setVersionValueId(dto.getVersionValueId());
				view.setDescription(dto.getDescription());
				if( dto.getNumericValue() == -1 ){
					view.setNumericValue(StringUtils.EMPTY);
				}else{
					view.setNumericValue(NumberUtil.convertPrice(dto.getNumericValue()));
				}
				view.setStringValue(dto.getStringValue());
			}
		}catch( Exception exception ){
			VersionsValuesException versionsValuesException=null;
			exception.printStackTrace(System.out);
			versionsValuesException= new VersionsValuesException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw versionsValuesException;
		}
		return view;
	}
	/**
	 * Versions Values Converter View to DTO
	 * @param view
	 * @return
	 * @throws VersionsValuesException
	 */
	public VersionsValuesDTO convertVersionsValuesViewToDto(VersionsValuesView view) throws VersionsValuesException{
		VersionsValuesDTO dto = null;
		try{
			if(view != null){
				dto = new VersionsValuesDTO();
				dto.setVersionValueId(view.getVersionValueId());
				dto.setNumericValue(NumberUtil.parseDouble(view.getNumericValue()));
				dto.setStringValue(view.getStringValue());
			}
		}catch( Exception exception ){
			VersionsValuesException versionsValuesException=null;
			exception.printStackTrace(System.out);
			versionsValuesException= new VersionsValuesException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw versionsValuesException;
		}
		return dto;
	}	
	/**
	 * Versions Values DTO List to Views
	 * @param versionsValuesDTOs
	 * @return
	 * @throws VersionsValuesException
	 */
	public List convertDTOsToViews(List versionsValuesDTOs) throws VersionsValuesException{
		Iterator iterator        = null;
		List versionsValuesList  = null;
		VersionsValuesView view  = null;
		VersionsValuesDTO dto    = null;
		try{
			versionsValuesList = new ArrayList(0);
			iterator = versionsValuesDTOs.iterator();
			while( iterator.hasNext() ){
				dto = (VersionsValuesDTO) iterator.next();
				view = this.converterVersionsValuesDtoToView(dto);
				versionsValuesList.add(view);
			}			
		}catch( Exception exception ){
			VersionsValuesException versionsValuesException=null;
			exception.printStackTrace(System.out);
			versionsValuesException= new VersionsValuesException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw versionsValuesException;
		}
		return versionsValuesList;
	}
	/**
	 * Versions Values View List to DTO
	 * @param verionsValuesViews
	 * @return
	 * @throws VersionsValuesException
	 */
	public List convertViewToDTOs(List verionsValuesViews) throws VersionsValuesException{
		Iterator iterator        = null;
		List versionsValuesList  = null;
		VersionsValuesView view  = null;
		VersionsValuesDTO dto    = null;
		try{
			versionsValuesList = new ArrayList(0);
			iterator = verionsValuesViews.iterator();
			while( iterator.hasNext() ){
				view = (VersionsValuesView) iterator.next();				
				dto = this.convertVersionsValuesViewToDto(view);												
				versionsValuesList.add(dto);
			}			
		}catch( Exception exception ){
			VersionsValuesException versionsValuesException=null;
			exception.printStackTrace(System.out);
			versionsValuesException= new VersionsValuesException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw versionsValuesException;
		}
		return versionsValuesList;
	}
}
